<?php 
/**
* teacher 学生管理
*/
class Teacher_student extends CI_Controller
{
	
	function __construct()
	{
		parent::__construct();
		//登陆状态检测
		if(!$this->session->userdata('user_in'))
			redirect('home');
		//权限检测
		if($this->session->userdata('role_id') != 2)
			redirect('home');
	}

	/**
	 * student管理的主界面
	 * @return [type]
	 */
	function index()
	{
		//准备数据
		$data = array();
		$data['title']='学生管理';

		$this->load->model('student_model');
		$this->load->model('major_model');
		$this->load->model('class_model');
		$this->load->model('logic_class_model');

		$this->load->model('teacher_model');
		$teacher=$this->teacher_model->get_by_username($this->session->userdata('username'));
		
		$logic_classes=$this->logic_class_model->get_logic_classes_by_teacher_id($teacher['id']);
		$index=0;
		foreach ($logic_classes as $item) {
			$classes_list_by_logic_class[$index]=$this->class_model->get_classes_by_logic_class_id($item['id']);
			
			for ($i=0; $i < count($classes_list_by_logic_class[$index]); $i++) { 
				$classes_list_by_logic_class[$index][$i]['select_num']=$this->class_model->get_select_num_in_class($item['id'],$classes_list_by_logic_class[$index][$i]['id']);
				$classes_list_by_logic_class[$index][$i]['total_num']=$this->class_model->get_total_num_in_class($classes_list_by_logic_class[$index][$i]['id']);
			}
			
			$index++;	
		}

		$data['logic_classes']=$logic_classes;
		$data['classes_list_by_logic_class']=$classes_list_by_logic_class;
		//var_dump($classes_list_by_logic_class);
		$this->load->view('teacher_student/index',$data);
	}     

	/**
	 * 查看某个逻辑班下，某个班级里的学生
	 * @return [type] [description]
	 */
	function check_class($logic_class_id,$class_id)
	{
		$this->load->model('logic_class_model');
		$this->load->model('class_model');
		$logic_class=$this->logic_class_model->get_logic_class_by_id($logic_class_id);
		if(!$logic_class) return $this->load->view('error');
		$class=$this->class_model->get_by_id($class_id);
		if(!$class_id) return $this->load->view('error');

		$this->load->model('student_model');
		$students=$this->student_model->get_students_by_logic_class_id_and_class_id($logic_class_id,$class_id);
		$this->load->model('teacher_model');
		$me=$this->teacher_model->get_by_username($this->session->userdata('username'));
		for ($i=0; $i < count($students); $i++) { 
			$students[$i]['editable']=$this->student_model->check_student_editable($students[$i]['id'],$me['id']);
		}
		

		$data['logic_class']=$logic_class;
		$data['class']=$class;
		$data['students']=$students;
		$data['title']='查看学生';
		$this->load->view('teacher_student/check_class',$data);
	}

	/**
	 * 删除某个逻辑班下，某个班级里的学生
	 * @return [type] [description]
	 */
	function delete_class($logic_class_id,$class_id)
	{
		$this->load->model('logic_class_model');
		$this->load->model('class_model');
		$logic_class=$this->logic_class_model->get_logic_class_by_id($logic_class_id);
		// var_dump($logic_class_id);
		// var_dump($class_id);
		if(!$logic_class) return $this->load->view('error');
		$class=$this->class_model->get_by_id($class_id);
		if(!$class_id) return $this->load->view('error');

		$this->load->model('student_model');
		$students=$this->student_model->get_students_by_logic_class_id_and_class_id($logic_class_id,$class_id);
		//var_dump($students);
		$this->load->model('teacher_model');
		$me=$this->teacher_model->get_by_username($this->session->userdata('username'));
		for ($i=0; $i < count($students); $i++) { 
			$students[$i]['editable']=$this->student_model->check_student_editable($students[$i]['id'],$me['id']);
		}

		// var_dump($logic_class);
		// var_dump($class);
		// var_dump($students);
		$students_num=0;
		$delete_num=0;

		foreach ($students as $students_item) {
			$students_num++;
			if($this->student_model->delete_from_logic_class($logic_class_id,$students_item['id']))
				//如果这个学生并没有选择其他课程，则删除这个学生
				if(!$this->student_model->check_in_logic_class($students_item['id']))
				{
					$this->student_model->delete_cascade($students_item['id']);
				}
				$delete_num++;
		}

		if($students_num==$delete_num)
			$this->session->set_flashdata('message','操作成功');
		else
			$this->session->set_flashdata('message','存在相关引用，未完全删除');
		redirect('teacher_student/index');
	}

	/**
	 * 添加学生
	 */
	function add()
	{
		//表单验证
		if($this->form_validation->run('teacher_student/add'))
		{
			//post方法
			//读取数据
			$student_name=$this->input->post('student_name');
			$student_id=$this->input->post('student_id');
			$major_id=$this->input->post('major_id');
			$class_id=$this->input->post('class_id');
			$logic_classes=$this->input->post('logic_classes');

			//如果用户没有选择课程（逻辑班），则报错
			if(!$logic_classes)
			{
				$data['error_message']='您必须为这个学生选择一个您的课程';
				return $this->load->view('error',$data);
			}

			//如果这个学生已经存在，核对信息
			$this->load->model('student_model');
			if($this->student_model->student_id_exist($student_id))
			{
				$student=$this->student_model->get_by_student_id($student_id);
				if($student['name']!=$student_name||$student['major_id']!=$major_id||$student['class_id']!=$class_id)
				{
					$data['error_message']=($student['class_id']!=$class_id).'这个学号的学生已经存在，系统尝试合并信息时出现错误，请检查您输入的姓名，专业，班级信息是否正确。';
					return $this->load->view('error',$data);
				}
				//添加学生选课关系
				$student_id=$student['id'];
				if($this->student_model->add_relative_logic_classes($student_id,$logic_classes))
				{
					$this->session->set_flashdata('message','添加成功');
					redirect('teacher_student/index');	
				}
			}
			else
			{
				//新建一个用户
				$user = array(
					'username' => $student_id,
					'password' => md5($student_id),
					'role_id' => 3,
					 );

				$this->load->model('user_model');
				if($this->user_model->add($user))
				{
					$user_id=$this->db->insert_id();

					//将学生写入数据库
					$student = array(
						'user_id' => $user_id,
						'name' => $student_name,
						'student_id' => $student_id,
						'major_id' => $major_id,
						'type' => 1,
						'class_id' => $class_id,
						 );
					$this->load->model('student_model');
					if($this->student_model->add($student))
					{
						//添加学生选课关系
						$student_id=$this->db->insert_id();
						if($this->student_model->add_relative_logic_classes($student_id,$logic_classes))
						{
							$this->session->set_flashdata('message','添加成功');
							redirect('teacher_student/index');	
						}
						
					}
				}
			}

			
			

			$this->session->set_flashdata('message','操作失败');
			redirect('teacher_student/index');
					
		}

		//get方法
		$data['title']='添加学生';
		$this->load->model('major_model');
		$this->load->model('class_model');
		$this->load->model('logic_class_model');
		$this->load->model('teacher_model');
		$me=$this->teacher_model->get_by_username($this->session->userdata('username'));

		$data['majors']=$this->major_model->get_all();
		$data['classes']=$this->class_model->get_all();
		$data['logic_classes']=$this->logic_class_model->get_all_logic_classes_by_teacher_id($me['id']);

		$this->load->view('teacher_student/add',$data);
	}
	
	/**
	 * 批量添加学生
	 */
	function batch_add()
	{
		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'text|txt';
		$config['max_size'] = '1000';
		$config['file_name'] = date('ymdHis',time());
		$this->load->library('upload', $config);

		if (!$this->upload->do_upload('student_file'))
		{
			$data['error_message']='文件不符合要求';
			$this->load->view('error',$data);
		} 
		else
		{
			
			$file_info=$this->upload->data();
			$content=file_get_contents($file_info['full_path']);
			$content=$this->remove_utf8_bom($content);
			// $content = mb_convert_encoding($content, "UTF-8");
			header('Content-Type: text/html; charset=UTF-8');
			$first_line=strtok($content, "\n");
			$second_line=strtok("\n");
			$third_line=strtok("\n");

			

			$item=strtok("\n");
			$items=array();
			while($item)
			{
				array_push($items,$item);
				$item=strtok("\n");
			}

			$teacher_name=strtok($first_line, ",");
			$teacher_name=substr($teacher_name, 1);
			$teacher_name=trim($teacher_name);
			$teacher_number=strtok("\n");
			$teacher_number=trim($teacher_number);
			$this->load->model('teacher_model');
			$teacher_exist=$this->teacher_model->teacher_number_exist($teacher_number);
			
			$teacher_state=FALSE;
			// if(!$teacher_exist) 
			// 	$teacher_state=TRUE;
			// else 
			// {
			// 	$teacher=$this->teacher_model->get_by_teacher_number($teacher_number);
			// 	if($teacher_name==$teacher['name'])
			// 		$teacher_state=TRUE;
			// }
			
			$me=$this->teacher_model->get_by_username($this->session->userdata('username'));
			if(!$teacher_exist) 
				$teacher_state=FALSE;
			else 
			{
				$teacher=$this->teacher_model->get_by_teacher_number($teacher_number);
				
				if($teacher_name==$teacher['name']&&$teacher['teacher_number']==$me['teacher_number'])
					$teacher_state=TRUE;
			}
				
			
			$logic_class_name=strtok($second_line, ",");
			$logic_class_name=trim($logic_class_name);
			$logic_class_name=substr($logic_class_name, 1);
			$logic_class_name=trim($logic_class_name);
			$logic_class_number=strtok("\n");
			$logic_class_number=trim($logic_class_number);
			$class_name=strtok($third_line, ",");
			$class_name=substr($class_name, 1);
			$class_name=trim($class_name);
			$this->load->model('class_model');
			$class_exist=$this->class_model->check_class_name_exist($class_name);

			$this->load->model('logic_class_model');
			$logic_class_exist=$this->logic_class_model->logic_class_number_exist($logic_class_number);

			$logic_class_state=FALSE;
			//检查课程名称是否存在
			$this->load->model('lecture_model');
			$lecture_exist=$this->lecture_model->check_lecture_exist($logic_class_name);
			if(!$lecture_exist)
				$logic_class_state=FALSE;
			else{
				if(!$logic_class_exist)
					$logic_class_state=TRUE;
				else
				{

					$logic_class=$this->logic_class_model->get_by_logic_class_number($logic_class_number);
					if($logic_class_name==$logic_class['lecture_name']&&$logic_class['teacher_id']==$me['id'])
					$logic_class_state=TRUE;
				}
			}
			
			
			

			$major_name=strtok("\n");
			$major_name=trim($major_name);
			$this->load->model('major_model');
			$major_exist=$this->major_model->check_major_name_exist($major_name);
			//$major_exist=1;
			//var_dump($major_name);
			//var_dump($major_exist);

			$this->load->model('student_model');
			$students=array();
			foreach ($items as $item) {
				$student['student_id']=strtok($item, ',');
				$student['student_id']=trim($student['student_id']);
				$student['student_name']=strtok("\n");
				$student['student_name']=trim($student['student_name']);
				$student['student_exist']=$this->student_model->student_id_exist($student['student_id']);

				$student['student_state']=FALSE;
				if(!$student['student_exist'])
					$student['student_state']=TRUE;
				else
				{
					$stu=$this->student_model->get_by_student_id($student['student_id']);
					if($student['student_name']==$stu['name'])
					$student['student_state']=TRUE;
				}
				if(!($student['student_id']&&$student['student_name']))
				{
					$data['error_message']='文件信息不完整，或格式错误1';
					$this->load->view('error',$data);
				}
				array_push($students, $student);
			}

			if(!($teacher_name&&$teacher_number&&$logic_class_name&&$logic_class_number&&$class_name&&$major_name))
			{
				$data['error_message']='文件信息不完整，或格式错误2';
				return $this->load->view('error',$data);
			}	
			$data['teacher_name']=$teacher_name;
			$data['teacher_number']=$teacher_number;
			$data['teacher_exist']=$teacher_exist;
			$data['teacher_state']=$teacher_state;

			$data['logic_class_name']=$logic_class_name;
			$data['logic_class_number']=$logic_class_number;
			$data['logic_class_exist']=$logic_class_exist;
			$data['logic_class_state']=$logic_class_state;

			$data['class_name']=$class_name;
			$data['class_exist']=$class_exist;
			$data['major_name']=$major_name;
			$data['major_exist']=$major_exist;
			$data['students']=$students;			
			$data['title']='批量添加预览';
			//var_dump($data);
			$this->load->view('teacher_student/batch_add_preview',$data);
			



		// $data = array('upload_data' => $this->upload->data());

		// $this->load->view('upload_success', $data);
		}
	}

	/**
	 * 检测并移除字符串中的bom
	 * @param  [type] $text [description]
	 * @return [type]       [description]
	 */
	function remove_utf8_bom($text)
	{
	    $bom = pack('H*','EFBBBF');
	    $text = preg_replace("/^$bom/", '', $text);
	    return $text;
	}
	/**
	 * 批量添加学生确认
	 * @return [type] [description]
	 */
	function batch_add_confirm()
	{
		//加载需要的模型
		$this->load->model('teacher_model');
		$this->load->model('class_model');
		$this->load->model('logic_class_model');
		$this->load->model('major_model');
		$this->load->model('user_model');
		$this->load->model('student_model');

		//读取数据
		$teacher_name=$this->input->post('teacher_name');
		$teacher_number=$this->input->post('teacher_number');
		$logic_class_name=$this->input->post('logic_class_name');
		$logic_class_number=$this->input->post('logic_class_number');
		$class_name=$this->input->post('class_name');
		$major_name=$this->input->post('major_name');
		$student_count=$this->input->post('student_count');
		
		//验证信息是否完整
		if(!($teacher_name&&$teacher_number&&$logic_class_name&&$logic_class_number&&$class_name&&$major_name))
		{
			$data['error_message']='文件信息不完整，或格式错误3';
			return $this->load->view('error',$data);
		}	


		
		$teacher_exist=$this->teacher_model->teacher_number_exist($teacher_number);
		$class_exist=$this->class_model->check_class_name_exist($class_name);
		$logic_class_exist=$this->logic_class_model->logic_class_number_exist($logic_class_number);
		$major_exist=$this->major_model->check_major_name_exist($major_name);

		//如果专业不存在错误
		if(!$major_exist)
		{
			$data['error_message']='专业不存在';
			return $this->load->view('error',$data);
		}
		$major=$this->major_model->get_by_major_name($major_name);

		//如果班级不存在，错误
		if(!$class_exist)
		{
			$data['error_message']='班级不存在';
			return $this->load->view('error',$data);
		}
		$class=$this->class_model->get_by_class_name($class_name);

		//如果老师不存在，错误
		if(!$teacher_exist)
		{
			$data['error_message']='这个老师不存在';
			return $this->load->view('error',$data);
		}
		$teacher=$this->teacher_model->get_by_teacher_number($teacher_number);
		$this->load->model('teacher_model');
		$me=$this->teacher_model->get_by_username($this->session->userdata('username'));
		//如果不是自己，错误
		if($teacher['teacher_number']!=$me['teacher_number'])
		{
			$data['error_message']='必须添加自己的学生';
			return $this->load->view('error',$data);
		}

		//逻辑班如果不存在新建一个
		if(!$logic_class_exist)
		{
			//通过名称得到课程id
			$lecture_name=$logic_class_name;
			$this->load->model('lecture_model');
			$lecture_id=$this->lecture_model->get_lecture_id($lecture_name);
			$logic_class = array(
				'number' => $logic_class_number,
				'teacher_id' => $teacher['id'],
				'type'=>1,
				'lecture_id'=>$lecture_id
				 );
			$this->logic_class_model->add($logic_class);
		}
		$logic_class=$this->logic_class_model->get_by_logic_class_number($logic_class_number);


		for ($i=1; $i <= $student_count; $i++) { 
			$student_name=$this->input->post('student_name_'.$i);
			$student_id=$this->input->post('student_id_'.$i);
			$student_exist=$this->student_model->student_id_exist($student_id);
			//如果存在这个用户，合并
			// if($student_exist)
			// {
			// 	$student=$this->student_model->get_by_student_id($student_id);
			// 	$this->student_model->delete_cascade($student['id']);
			// }
			
			//如果不存在新建一个
			if(!$student_exist)
			{
				//新建一个用户
				$user = array(
					'username' => $student_id,
					'password' => md5($student_id),
					'role_id' => 3,
					 );

				if($this->user_model->add($user))
				{
					$user_id=$this->db->insert_id();

					//将学生写入数据库
					$student = array(
						'user_id' => $user_id,
						'name' => $student_name,
						'student_id' => $student_id,
						'major_id' => $major['id'],
						'type' => 1,
						'class_id' => $class['id'],
						 );
					$this->student_model->add($student);
				}
			}	
			
			
			$student=$this->student_model->get_by_student_id($student_id);

			//录入选课信息
			$this->student_model->add_logic_class($student['id'],$logic_class['id']);

		}

		$this->session->set_flashdata('message','操作成功');
		redirect('teacher_student/index');

	}


	function cut_last_char($str)
	{
		return substr($str, 0,strlen($str)-1);
	}

	/**
	 * 查看一个学生的信息
	 * @param  integer $id [description]
	 * @return [type]      [description]
	 */
	function check_student($id=0,$logic_class_id,$class_id)
	{
		$this->load->model('student_model');
		$student=$this->student_model->get_by_id($id);
		if(!$student)
			return $this->load->view('error');

		$data['title']='查看学生';
		$data['before_logic_class_id']=$logic_class_id;
		$data['before_class_id']=$class_id;
		$data['student']=$this->student_model->get_student_by_id($id);
		$data['relative_logic_classes']=$this->student_model->get_relative_logic_classes($id);

		$this->load->view('teacher_student/check_student',$data);


	}

	/**
	 * 删除一个学生
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	function delete($logic_class_id,$class_id,$student_id)
	{

		$this->load->model('student_model');
		if(!$this->student_model->get_by_id($student_id))
			$this->load->view('error');
		if($this->student_model->delete_from_logic_class($logic_class_id,$student_id))
		{
			//如果这个学生并没有选择其他课程，则删除这个学生
			if(!$this->student_model->check_in_logic_class($student_id))
			{
				$this->student_model->delete_cascade($student_id);
			}
			$this->session->set_flashdata('message','操作成功');
		}
		else
			$this->session->set_flashdata('message','存在相关引用，操作失败');

		redirect('teacher_student/check_class'.'/'.$logic_class_id.'/'.$class_id);
	}

	/**
	 * 编辑一个学生的信息
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	function edit($id=0,$logic_class_id,$class_id)
	{
		if($id)
		{
			//get方法
			$this->load->model('student_model');
			$student=$this->student_model->get_student_by_id($id);
			if(!$student)
				return $this->load->view('error');
			$data['title']='编辑学生信息';
			$data['student']=$student;

			$this->load->model('major_model');
			$this->load->model('class_model');
			$this->load->model('logic_class_model');

			$data['logic_class_id']=$logic_class_id;
			$data['class_id']=$class_id;
			$data['majors']=$this->major_model->get_all();
			$data['classes']=$this->class_model->get_all();
			$this->load->model('teacher_model');
			$me=$this->teacher_model->get_by_username($this->session->userdata('username'));
			$data['logic_classes']=$this->logic_class_model->get_all_logic_classes_by_teacher_id($me['id']);
			$data['logic_classes_select']=$this->student_model->get_logic_classes_select($id);
			$this->load->view('teacher_student/edit',$data);
		}
		else
		{
			//post方法
			//读取数据
			$this->load->model('student_model');

			$id=$this->input->post('id');
			$before_logic_class_id=$this->input->post('before_logic_class_id');
			$before_class_id=$this->input->post('before_class_id');
			$student=$this->student_model->get_by_id($id);
			if(!$student)
				return $this->load->view('error');
			if($this->form_validation->run('teacher_student/edit'))
			{
				$student_name=$this->input->post('student_name');
				$major_id=$this->input->post('major_id');
				$class_id=$this->input->post('class_id');
				$logic_classes=$this->input->post('logic_classes');

				//将模块写入数据库
				$student = array(
					'name' => $student_name,
					'major_id' => $major_id,
					'class_id' => $class_id,
					 );
				
				//如果用户没有选择课程（逻辑班），则报错
				// if(!$logic_classes)
				// {
				// 	$data['error_message']='您必须为这个学生选择一个您的课程';
				// 	return $this->load->view('error',$data);
				// }


				//数据库操作
				//$this->student_model->delete_relative_logic_classes($id)&&$this->student_model->add_relative_logic_classes($id,$logic_classes)&&
				if($this->student_model->update($id,$student))
				{
					$this->session->set_flashdata('message','操作成功');
					redirect('teacher_student/check_class'.'/'.$before_logic_class_id.'/'.$before_class_id);
				}
				else
				{
					$this->session->set_flashdata('message','操作失败');
					redirect('teacher_student/check_class'.'/'.$before_logic_class_id.'/'.$before_class_id);
				}
	
			}
			else
			{
				//验证失败
				//get
				$data['title']='编辑学生信息';
				$data['student']=$student;

				$this->load->model('major_model');
				$this->load->model('class_model');
				$this->load->model('logic_class_model');

				$data['majors']=$this->major_model->get_all();
				$data['classes']=$this->class_model->get_all();
				$this->load->model('teacher_model');
				$me=$this->teacher_model->get_by_username($this->session->userdata('username'));
				$data['logic_classes']=$this->logic_class_model->get_all_logic_classes_by_teacher_id($me['id']);

				$this->load->view('teacher_student/edit',$data);
			}
				
		}
	}

	/**
	 * 查看一个学生的信息
	 * @param  integer $id [description]
	 * @return [type]      [description]
	 */
	function check_student_performance($id=0,$logic_class_id,$class_id)
	{
		$this->load->model('student_model');
		$this->load->model('major_model');
		$this->load->model('class_model');
		$this->load->model('report_model');
		$this->load->model('module_model');
		$this->load->model('lecture_model');
		$this->load->model('logic_class_model');


		$student=$this->student_model->get_by_id($id);
		if(!$student)
			return $this->load->view('error');

		//获取学生参加的逻辑班
		$student_logic_class=$this->student_model->get_student_logic_class($student['id']);
		
		// echo "student_logic_class";
		// var_dump($student_logic_class);
		//获取所有逻辑班的信息
		$all_logic_class = array();
		foreach ($student_logic_class as $item) {
			$logic_class=$this->logic_class_model->get_by_id($item['logic_class_id']);
			array_push($all_logic_class, $logic_class);
		}

		// echo "all_logic_class";
		// var_dump($all_logic_class);

		//获取逻辑班下的考试模块
		$all_logic_class_module = array();
		foreach ($all_logic_class as $item) {
			$module_in_logic_class=$this->module_model->get_module_in_logic_class($item['lecture_id']);
			// echo "module_in_logic_class";
			// var_dump($module_in_logic_class);
			$lecture_info = $this->lecture_model->get_lecture_by_id($item['lecture_id']);
			//所有系统的模块和逻辑班信息叠加
			$module_add_logic_class = array();
			foreach ($module_in_logic_class as $item1) {

				//寻找每一个模块是否有可替换的定制模块
				$module_replace=$this->module_model->get_module_type_1_parent_teacher($item1['id'],$item['teacher_id']);
				// echo "module_replace";
				// var_dump($module_replace);

				//没有可替换的定制模块
				if (!$module_replace) {				
					
					//在report表中查找是否已经参加了考试					
					$report=$this->report_model->get_by_module_student_logic_class($item1['id'],$student['id'],$item['id']);
					// var_dump($item1['id']);
					// var_dump($student['id']);
					// var_dump($item['id']);
					//var_dump($report);
					//没有考试
					if (!$report) {
						$each_module_add_logic_class = array(
							'id' => $item1['id'], 
							'name' => $item1['name'], 
							'time_limit' => $item1['time_limit'], 
							'type' => $item1['type'], 
							'parent_id' => $item1['parent_id'], 
							'teacher_id' => $item1['teacher_id'], 
							'big_lecture_id' => $item1['big_lecture_id'],
							'logic_class_id' => $item['id'],
							'logic_class_number' => $item['number'],
							'logic_class_teacher_id' => $item['teacher_id'],
							'logic_class_type' => $item['type'],
							'lecture_id' => $item['lecture_id'],
							'lecture_name' => $lecture_info['name'],
							'big_lecture_name' => $lecture_info['big_lecture_name'],
							'report_id' => '0',
							'report_datetime' => '',
							'report_grades' => '',
							'report_state'=>2);
					}
					//已考试
					else{
						//检查当前的考试状态，根据时间判断，是否已经结束，还是可以继续进行
						if($report['state']==0)
						{
							//检查考试时间
							$now=date("Y-m-d H:i:s");
							$this->load->helper('compare_time');
							$time_past=compare_time_of_minute($report['datetime'],$now);
							if($time_past<=$item1['time_limit'])
							{
								//如果还有时间继续考试
							}
							else
							{
								//如果已经超过时间，判卷，更新report信息
								$this->refresh_grades($report['id']);
								$report=$this->report_model->get_by_module_student_logic_class($item1['id'],$student['id'],$item['id']);
							}
						}
						$each_module_add_logic_class = array(
							'id' => $item1['id'], 
							'name' => $item1['name'], 
							'time_limit' => $item1['time_limit'], 
							'type' => $item1['type'], 
							'parent_id' => $item1['parent_id'], 
							'teacher_id' => $item1['teacher_id'], 
							'big_lecture_id' => $item1['big_lecture_id'], 
							'logic_class_id' => $item['id'],
							'logic_class_number' => $item['number'],
							'logic_class_teacher_id' => $item['teacher_id'], 
							'logic_class_type' => $item['type'],
							'lecture_id' => $item['lecture_id'],
							'lecture_name' => $lecture_info['name'],
							'big_lecture_name' => $lecture_info['big_lecture_name'],
							'report_id' => $report['id'],
							'report_datetime' =>  $report['datetime'],
							'report_grades' =>  $report['grades'],
							'report_state'=>$report['state']);
					}
					array_push($module_add_logic_class, $each_module_add_logic_class);
				}

				//有替换的模块
				else{
					foreach ($module_replace as $item2) {
						$report_replace=$this->report_model->get_by_module_student_logic_class($item2['id'],$student['id'],$item['id']);
						//没有考试
						if (!$report_replace) {
							$each_module_add_logic_class_replace = array(
								'id' => $item2['id'], 
								'name' => $item2['name'], 
								'time_limit' => $item2['time_limit'], 
								'type' => $item2['type'], 
								'parent_id' => $item2['parent_id'], 
								'teacher_id' => $item2['teacher_id'], 
								'big_lecture_id' => $item2['big_lecture_id'],
								'logic_class_id' => $item['id'],
								'logic_class_number' => $item['number'],
								'logic_class_teacher_id' => $item['teacher_id'],
								'logic_class_type' => $item['type'],
								'lecture_id' => $item['lecture_id'],
								'lecture_name' => $lecture_info['name'],
								'big_lecture_name' => $lecture_info['big_lecture_name'],
								'report_id' => '0',
								'report_datetime' => '',
								'report_grades' => '',
								'report_state'=>2
								);
								

						}
						//已考试
						else{
							if($report_replace['state']==0)
							{
								//检查考试时间
								$now=date("Y-m-d H:i:s");
								$this->load->helper('compare_time');
								$time_past=compare_time_of_minute($report_replace['datetime'],$now);
								if($time_past<=$item1['time_limit'])
								{
									//如果还有时间继续考试
								}
								else
								{
									//如果已经超过时间，判卷，更新report信息
									$this->refresh_grades($report_replace['id']);
									$report_replace=$this->report_model->get_by_module_student_logic_class($item2['id'],$student['id'],$item['id']);
								}
							}
							$each_module_add_logic_class_replace = array(
								'id' => $item2['id'], 
								'name' => $item2['name'], 
								'time_limit' => $item2['time_limit'], 
								'type' => $item2['type'], 
								'parent_id' => $item2['parent_id'], 
								'teacher_id' => $item2['teacher_id'], 
								'big_lecture_id' => $item2['big_lecture_id'],
								'logic_class_id' => $item['id'],
								'logic_class_number' => $item['number'],
								'logic_class_teacher_id' => $item['teacher_id'],
								'logic_class_type' => $item['type'],
								'lecture_id' => $item['lecture_id'],
								'lecture_name' => $lecture_info['name'],
								'big_lecture_name' => $lecture_info['big_lecture_name'],
								'report_id' => $report_replace['id'],
								'report_datetime' =>  $report_replace['datetime'],
								'report_grades' =>  $report_replace['grades'],
								'report_state'=>$report_replace['state']
							);
						}
						
						array_push($module_add_logic_class, $each_module_add_logic_class_replace);
					}				
					
				}
				
			}
			// echo "module_add_logic_class";
			// var_dump($module_add_logic_class);		
			
			array_push($all_logic_class_module, $module_add_logic_class);
		}

		//var_dump($all_logic_class_module);

		$major=$this->major_model->get_by_id($student['major_id']);
		$class=$this->class_model->get_by_id($student['class_id']);
		
		$data = array();
		$data['title']='查看学生';
		$data['before_logic_class_id']=$logic_class_id;
		$data['before_class_id']=$class_id;
		$data['student']=$this->student_model->get_student_by_id($id);
		$data['major']=$major;
		$data['class']=$class;
		$data['all_logic_class_module']=$all_logic_class_module;
		$data['relative_logic_classes']=$this->student_model->get_relative_logic_classes($id);

		$this->load->view('teacher_student/check_student_performance',$data);



	}

	/**
	 * 查询特定的某些同学
	 * @return [type] [description]
	 */
	function search()
	{
		$major_id=$this->input->post('major_id');
		$class_id=$this->input->post('class_id');
		$logic_class_id=$this->input->post('logic_class_id');
		$student_type=$this->input->post('student_type');

		$this->load->model('student_model');
		$students=$this->student_model->search($major_id,$class_id,$logic_class_id,$student_type);

		$this->load->model('major_model');
		$this->load->model('class_model');
		$this->load->model('logic_class_model');
		$data['majors']=$this->major_model->get_all();
		$data['classes']=$this->class_model->get_all();
		$data['logic_classes']=$this->logic_class_model->get_all_logic_classes();
		$data['students']=$students;

		$data['major_id_select']=$major_id;
		$data['class_id_select']=$class_id;
		$data['logic_class_id_select']=$logic_class_id;
		$data['student_type_select']=$student_type;
		$data['title']='查询';
		$this->load->view('teacher_student/index',$data);
	}

	//重置学生的密码
	function reset_password($logic_class_id,$class_id,$id=0)
	{
		$this->load->model('student_model');
		$student = $this->student_model->get_by_id($id);
		if(!$student)
			$this->load->view('error');
		var_dump($student);
		$username = $student['student_id'];
		$this->load->model('user_model');
		$user=$this->user_model->get_by_username($username);
		var_dump($user);
		if($user)
		{
			$user['password']=md5($username);
			if($this->user_model->update($user['id'],$user))
			{
				$this->session->set_flashdata('message','修改成功');
				redirect('teacher_student/check_class'.'/'.$logic_class_id.'/'.$class_id);
			}
			else
			{
				$this->session->set_flashdata('message','操作失败');
				redirect('teacher_student/check_class'.'/'.$logic_class_id.'/'.$class_id);
			}
					
		}
		else{
			$this->session->set_flashdata('message','不存在该用户');
			redirect('teacher_student/check_class'.'/'.$logic_class_id.'/'.$class_id);
		}
	}

	//获取考试模块下的所有题目的相关的知识点，包括考点信息，返回以考点为单位分组的二维数组
	function get_all_knowledge_point_with_test_point_by_module($module_id)
	{
		$test_point_in_module = $this->test_point_model->get_by_module($module_id);
		if(!$test_point_in_module)
        return false;
		//var_dump($test_point_in_module);
		//获取考点下的所有知识点并组合到一个数组当中
		$all_knowledge_point = array();
		foreach ($test_point_in_module as $test_point_in_module_item) {
			$knowledge_point_of_test_point=$this->knowledge_point_model->get_knowledge_point_by_question_of_test_point($test_point_in_module_item['test_point_id']);
			//$knowledge_point_of_test_point['module_name']=$module['name'];
			foreach ($knowledge_point_of_test_point as $knowledge_point_of_test_point_item) {
				array_push($all_knowledge_point, $knowledge_point_of_test_point_item);
			}
			
		}
		return $all_knowledge_point;
	}

	//返回所有知识点分类的题目
	function get_questions_group_by_knowledge($all_knowledge_point)
	{
		$questions_group_by_knowledge = array();//存储按照知识点分类的题目的数组
		foreach ($all_knowledge_point as $all_knowledge_point_item) {
			$questions_of_test_point_and_knowledge_point = $this->question_model->get_by_test_point_and_knowledge_point($all_knowledge_point_item['test_point_id'],$all_knowledge_point_item['knowledge_point_id']);
			//$questions_of_test_point_and_knowledge_point['test_point_id']=$all_knowledge_point_item['test_point_id'];
			//$questions_of_test_point_and_knowledge_point['knowledge_point_id']=$all_knowledge_point_item['knowledge_point_id'];
			array_push($questions_group_by_knowledge, $questions_of_test_point_and_knowledge_point);

		}
		return $questions_group_by_knowledge;
	}

	/**
	 * 查看某个模块下，以班级为单位统计该课程下的所有班级的知识点错误情况
	 * @return [type] [description]
	 */
	function check_knowledge_points_in_module_of_class($teacher_id,$logic_class_id,$module_id)
	{
		$this->load->model('logic_class_model');
		$this->load->model('module_model');
		$this->load->model('teacher_model');
		$this->load->model('test_point_model');
		$this->load->model('knowledge_point_model');
		$this->load->model('question_model');
		$this->load->model('class_model');
		$this->load->model('student_model');
		$this->load->model('report_model');
		$logic_class=$this->logic_class_model->get_logic_class_by_id($logic_class_id);
		$teacher=$this->teacher_model->get_by_id($teacher_id);
		$module=$this->module_model->get_by_id($module_id);
		$all_knowledge_point = $this->get_all_knowledge_point_with_test_point_by_module($module_id);
		if(!$all_knowledge_point)
        return false;
    	//获取按照知识点分组的题目
		$questions_group_by_knowledge = $this->get_questions_group_by_knowledge($all_knowledge_point);
		if(!$questions_group_by_knowledge)
        return false;

		//查找逻辑班中的所有班级
		$classes_list_by_logic_class=$this->class_model->get_classes_by_logic_class_id($logic_class_id);
			
		for ($i=0; $i < count($classes_list_by_logic_class); $i++) 
		{ 
			$classes_list_by_logic_class[$i]['select_num']=$this->class_model->get_select_num_in_class($logic_class_id,$classes_list_by_logic_class[$i]['id']);
			$classes_list_by_logic_class[$i]['total_num']=$this->class_model->get_total_num_in_class($classes_list_by_logic_class[$i]['id']);
		}
		if(!$classes_list_by_logic_class)
        return false;
		//echo "班级列表";
		//var_dump($classes_list_by_logic_class);
		//获取每个班级中的所有学生
		$index=0;
		foreach ($classes_list_by_logic_class as $classes_list_by_logic_class_item) {
			$students=$this->student_model->get_students_by_logic_class_id_and_class_id($logic_class_id,$classes_list_by_logic_class_item['id']);
			$students_in_classes_list_by_logic_class[$index]=$students;
			//var_dump($students);
			++$index;
		}
		if(!$students_in_classes_list_by_logic_class)
        return false;

		//获取在某个模块考试中某学生的某道题是不是做错了
		$class_num=0;
		$class_knowledge_num=0;
		$wrong_num=0;
		$student_num_in_report = array();
		$student_num_in_report_count = 0;

		foreach ($students_in_classes_list_by_logic_class as $students_in_classes_list_by_logic_class_item) 
		{
			//拆分到每个班级
			//先计算每个班参加考试的学生的数量
			$student_num_in_report_of_class=0;

			foreach ($students_in_classes_list_by_logic_class_item as $students_in_classes_list_by_logic_class_item_item) 
			{
				//获取每个学生的考试分数
				$student_grade=$this->report_model->get_grade($students_in_classes_list_by_logic_class_item_item['id'],$module_id,$logic_class_id);
				if($student_grade)
					++$student_num_in_report_of_class;
			}			
			
			foreach ($questions_group_by_knowledge as $questions_group_by_knowledge_item) 
			{
				//拆分到每个知识点
				foreach ($questions_group_by_knowledge_item as $questions_group_by_knowledge_item_item) 
				{
					//拆分到每个题目
					foreach ($students_in_classes_list_by_logic_class_item as $students_in_classes_list_by_logic_class_item_item) 
					{
						//拆分到每个学生				
						$question_id=$questions_group_by_knowledge_item_item['id'];
						$student_id=$students_in_classes_list_by_logic_class_item_item['id'];
						$wrong_question=$this->report_model->get_student_wrong_question_by_module_logic_class($module_id,$logic_class_id,$student_id,$question_id);
						if($wrong_question)
							++$wrong_num;

					}
				}
				$wrong_num_of_class_knowledge[$class_num][$class_knowledge_num] = $wrong_num;
				++$class_knowledge_num;
				$wrong_num=0;
			}
			array_push($student_num_in_report, $student_num_in_report_of_class);
			$student_num_in_report_count=$student_num_in_report_count+$student_num_in_report_of_class;
			++$class_num;
			$class_knowledge_num=0;
		}
		//将参加考试的学生总数同样插入到数组尾部，便于在一列显示
		array_push($student_num_in_report,$student_num_in_report_count);
		//计算每个知识点错误数量的总和
		$wrong_num_of_knowledge_point = array();
		$wrong_percent_of_knowledge_point = array();
			
		for($i=0;$i<count($wrong_num_of_class_knowledge[0],0);++$i)
		{
			$wrong_num_of_each_knowledge_point=0;
			$wrong_percent_of_each_knowledge_point=0;
			foreach ($wrong_num_of_class_knowledge as $wrong_num_of_class_knowledge_item) 
			{
				$wrong_num_of_each_knowledge_point=$wrong_num_of_each_knowledge_point+$wrong_num_of_class_knowledge_item[$i];
			}
			
			if($student_num_in_report_count)
			{
				$wrong_percent_of_each_knowledge_point=round($wrong_num_of_each_knowledge_point/$student_num_in_report_count*100,2).'%';
			}
			else
			{
				$wrong_percent_of_each_knowledge_point=0;
			}
			array_push($wrong_num_of_knowledge_point, $wrong_num_of_each_knowledge_point);
			array_push($wrong_percent_of_knowledge_point, $wrong_percent_of_each_knowledge_point);
		}
		//将知识点错误数目和百分比数插入到学生的知识点错误数目数组中，以便导出到excel表格
		array_push($wrong_num_of_class_knowledge, $wrong_num_of_knowledge_point);
		array_push($wrong_num_of_class_knowledge, $wrong_percent_of_knowledge_point);
		//var_dump($wrong_num_of_class_knowledge);
		$this->handle_class_excel($all_knowledge_point,$classes_list_by_logic_class,$wrong_num_of_class_knowledge,$logic_class,$module,$student_num_in_report);
	}

//以学生为单位，查看班级内的学生知识点错误情况
	function check_knowledge_points_in_module_of_student_in_class($teacher_id,$logic_class_id,$module_id,$class_id)
	{
		$this->load->model('logic_class_model');
		$this->load->model('module_model');
		$this->load->model('teacher_model');
		$this->load->model('test_point_model');
		$this->load->model('knowledge_point_model');
		$this->load->model('question_model');
		$this->load->model('class_model');
		$this->load->model('student_model');
		$this->load->model('report_model');
		$class=$this->class_model->get_by_id($class_id);
		$logic_class=$this->logic_class_model->get_logic_class_by_id($logic_class_id);
		$teacher=$this->teacher_model->get_by_id($teacher_id);
		$module=$this->module_model->get_by_id($module_id);
		$all_knowledge_point = $this->get_all_knowledge_point_with_test_point_by_module($module_id);
		if(!$all_knowledge_point)
        return false;
    	//获取按照知识点分组的题目
		$questions_group_by_knowledge = $this->get_questions_group_by_knowledge($all_knowledge_point);
		if(!$questions_group_by_knowledge)
        return false;
    	$students=$this->student_model->get_students_by_logic_class_id_and_class_id($logic_class_id,$class_id);
    	//var_dump($students);
    	if(!$students)
        return false;

    	$student_num=0;
		$student_knowledge_num=0;
		$wrong_num=0;
		$student_in_report_num=0;
		$students_grade = array();
		
		foreach ($students as $students_item) 
		{
			//拆分到每个学生
			//获取每个学生的考试分数
			$student_grade=$this->report_model->get_grade($students_item['id'],$module_id,$logic_class_id);
			if($student_grade)
				++$student_in_report_num;
			array_push($students_grade, $student_grade);
			foreach ($questions_group_by_knowledge as $questions_group_by_knowledge_item) 
			{
				//拆分到每个知识点
				foreach ($questions_group_by_knowledge_item as $questions_group_by_knowledge_item_item) 
				{
					//拆分到每个题目
						//var_dump($questions_group_by_knowledge_item_item);
						//var_dump($students_in_classes_list_by_logic_class_item_item);
						$question_id=$questions_group_by_knowledge_item_item['id'];
						$student_id=$students_item['id'];
						$wrong_question=$this->report_model->get_student_wrong_question_by_module_logic_class($module_id,$logic_class_id,$student_id,$question_id);
						if($wrong_question)
							++$wrong_num;
				}
				$wrong_num_of_student_knowledge[$student_num][$student_knowledge_num] = $wrong_num;
				++$student_knowledge_num;
				$wrong_num=0;
			}
		++$student_num;
		$student_knowledge_num=0;
		}
		//将参加考试的学生人数的总数插入到最后，以便在excel表格中同一列显示
		array_push($students_grade, $student_in_report_num);
		//计算每个知识点的错误总数,并计算占参加考试的学生数目的百分比
		$wrong_num_of_knowledge_point = array();
		$wrong_percent_of_knowledge_point = array();
			
		for($i=0;$i<count($wrong_num_of_student_knowledge[0],0);++$i)
		{
			$wrong_num_of_each_knowledge_point=0;
			$wrong_percent_of_each_knowledge_point=0;
			foreach ($wrong_num_of_student_knowledge as $wrong_num_of_student_knowledge_item) 
			{
				$wrong_num_of_each_knowledge_point=$wrong_num_of_each_knowledge_point+$wrong_num_of_student_knowledge_item[$i];
			}
			if($student_in_report_num)
			{
				$wrong_percent_of_each_knowledge_point=round($wrong_num_of_each_knowledge_point/$student_in_report_num*100,2).'%';
			}
			else
			{
				$wrong_percent_of_each_knowledge_point=0;
			}
			
			array_push($wrong_num_of_knowledge_point, $wrong_num_of_each_knowledge_point);
			array_push($wrong_percent_of_knowledge_point, $wrong_percent_of_each_knowledge_point);
		}
		//将知识点错误数目和百分比数插入到学生的知识点错误数目数组中，以便导出到excel表格
		array_push($wrong_num_of_student_knowledge, $wrong_num_of_knowledge_point);
		array_push($wrong_num_of_student_knowledge, $wrong_percent_of_knowledge_point);
		$this->handle_student_excel($all_knowledge_point,$students,$wrong_num_of_student_knowledge,$logic_class,$module,$class,$students_grade);
	}

	//导出到表格
	function handle_class_excel($all_knowledge_point,$classes_list_by_logic_class,$wrong_num_of_class_knowledge,$logic_class,$module,$student_num_in_report)
	{
		// if(!$query)
        //return false;

	    // Starting the PHPExcel library
	    $this->load->library('PHPExcel');
	    $this->load->library('PHPExcel/IOFactory');

	    $objPHPExcel = new PHPExcel();
	    $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");

	    $objPHPExcel->setActiveSheetIndex(0);

	    // Field names in the first row
	    //$fields = $query->list_fields();
	    //$col 列从0开始。$row 行从1开始
	    $col = 2;
	    $test_point_nr=0;
	    foreach ($all_knowledge_point as $all_knowledge_point_item)
	    {	if ($all_knowledge_point_item['test_point_id']!=$test_point_nr) 
	    	{
	    		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, '('.$all_knowledge_point_item['test_point_id'].')'.$all_knowledge_point_item['test_point_content']);
	    	}
	    	$test_point_nr = $all_knowledge_point_item['test_point_id'];
	    	
	    	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 2, '('.$all_knowledge_point_item['knowledge_point_id'].')'.$all_knowledge_point_item['knowledge_point_content']);    	
	        $col++;
	    }

	    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, 1, '考点');
	    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, 2, '知识点');
	    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, 2, '参考人数');

	    $row = 3;

	    foreach ($student_num_in_report as $student_num_in_report_of_class)
	    {	
	    	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $row, $student_num_in_report_of_class);
	        $row++;
	    }

	    $row = 3;
	    $col = 0;
	    $count_name['name']="总和";
	    $percent_name['name']="百分比";
	    array_push($classes_list_by_logic_class, $count_name);
	    array_push($classes_list_by_logic_class, $percent_name);
	    foreach ($classes_list_by_logic_class as $classes_list_by_logic_class_item)
	    {	
	    	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $classes_list_by_logic_class_item['name']);
	        $row++;
	    }
	    
	    // Fetching the table data
	    $row = 3;
	    foreach($wrong_num_of_class_knowledge as $wrong_num_of_class_knowledge_item)
	    {
	        $col = 2;
	        foreach ($wrong_num_of_class_knowledge_item as $wrong_num_of_class_knowledge_item_value)
	        {
	            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $wrong_num_of_class_knowledge_item_value);
	            $col++;
	        }

	        $row++;
	    }

	    $objPHPExcel->setActiveSheetIndex(0);

	    $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');

	    // Sending headers to force the user to download the file
	    header('Content-Type: application/vnd.ms-excel');
	    header('Content-Disposition: attachment;filename=""'.$logic_class['lecture_name']."(".$logic_class['number'].")班 ".$module['name']."考试知识点错误情况统计.xls");
	    header('Cache-Control: max-age=0');

	    $objWriter->save('php://output');
	}

		//导出到表格
	function handle_student_excel($all_knowledge_point,$students,$wrong_num_of_student_knowledge,$logic_class,$module,$class,$students_grade)
	{
		// if(!$query)
        //return false;

	    // Starting the PHPExcel library
	    $this->load->library('PHPExcel');
	    $this->load->library('PHPExcel/IOFactory');

	    $objPHPExcel = new PHPExcel();
	    $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");

	    $objPHPExcel->setActiveSheetIndex(0);

	    // Field names in the first row
	    //$fields = $query->list_fields();
	    //$col 列从0开始。$row 行从1开始
	    $col = 3;
	    $test_point_nr=0;
	    foreach ($all_knowledge_point as $all_knowledge_point_item)
	    {	if ($all_knowledge_point_item['test_point_id']!=$test_point_nr) 
	    	{
	    		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1,  '('.$all_knowledge_point_item['test_point_id'].')'.$all_knowledge_point_item['test_point_content']);
	    	}
	    	$test_point_nr = $all_knowledge_point_item['test_point_id'];
	    	
	    	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 2,  '('.$all_knowledge_point_item['knowledge_point_id'].')'.$all_knowledge_point_item['knowledge_point_content']);    	
	        $col++;
	    }

	    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, 1, '考点');
	    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, 2, '知识点');
	    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, 2, '成绩');

	    $row = 3;
	    $count_name['student_id']=" ";
	    $percent_name['student_id']=" ";
	    $count_name['name']="总和";
	    $percent_name['name']="百分比";
	    array_push($students, $count_name);
	    array_push($students, $percent_name);
	    foreach ($students as $students_item)
	    {	
	    	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $row, $students_item['student_id']);
	    	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $row, $students_item['name']);
	        $row++;
	    }

	    $row = 3;

	    foreach ($students_grade as $student_grade)
	    {	
	    	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $row, $student_grade);
	        $row++;
	    }
	    
	    // Fetching the table data
	    $row = 3;
	    foreach($wrong_num_of_student_knowledge as $wrong_num_of_student_knowledge_item)
	    {
	        $col = 3;
	        foreach ($wrong_num_of_student_knowledge_item as $wrong_num_of_student_knowledge_item_value)
	        {
	            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $wrong_num_of_student_knowledge_item_value);
	            $col++;
	        }

	        $row++;
	    }

	    $objPHPExcel->setActiveSheetIndex(0);

	    $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');

	    // Sending headers to force the user to download the file
	    header('Content-Type: application/vnd.ms-excel');
	    header('Content-Disposition: attachment;filename=""'.$logic_class['lecture_name']."(".$logic_class['number'].")班 ".$class['name']."班 ".$module['name']."考试知识点错误情况统计.xls");
	    header('Cache-Control: max-age=0');

	    $objWriter->save('php://output');
	}
	/**
	 *查看逻辑班的考试模块
	 */
	function check_module_in_logic_class($logic_class_id,$class_id=0)
	{
		//获取这个逻辑班的课程中的所有考试模块
		$this->load->model('logic_class_model');
		$this->load->model('module_model');
		$this->load->model('lecture_model');
		$this->load->model('teacher_model');

		$teacher=$this->teacher_model->get_by_username($this->session->userdata('username'));
		$logic_class = $this->logic_class_model->get_by_id($logic_class_id);
		//var_dump($logic_class);
		$module_in_logic_class=$this->module_model->get_module_in_logic_class($logic_class['lecture_id']);
		//var_dump($module_in_logic_class);
		$lecture_info = $this->lecture_model->get_lecture_by_id($logic_class['lecture_id']);
		$module_add_logic_class=array();
		foreach ($module_in_logic_class as $module_in_logic_class_item) 
		{
			//寻找每一个模块是否有可替换的定制模块
			$module_replace=$this->module_model->get_module_type_1_parent_teacher($module_in_logic_class_item['id'],$teacher['id']);
			if($module_replace)
				{
					foreach ($module_replace as $module_replace_item) 
					{
						$each_module_add_logic_class_replace = array(
								'module_id' => $module_replace_item['id'], 
								'name' => $module_replace_item['name'], 
								'time_limit' => $module_replace_item['time_limit'], 
								'type' => $module_replace_item['type'], 
								'parent_id' => $module_replace_item['parent_id'], 
								'teacher_id' => $module_replace_item['teacher_id'], 
								'big_lecture_id' => $module_replace_item['big_lecture_id'],
								'logic_class_id' => $logic_class
								['id'],
								'logic_class_number' => $logic_class
								['number'],
								'logic_class_teacher_id' => $logic_class
								['teacher_id'],
								'logic_class_type' => $logic_class
								['type'],
								'lecture_id' => $logic_class
								['lecture_id'],
								'lecture_name' => $lecture_info['name'],
								'big_lecture_name' => $lecture_info['big_lecture_name']
								);
						array_push($module_add_logic_class, $each_module_add_logic_class_replace);
					}
					
				}
				else
				{
					$each_module_add_logic_class = array(
								'module_id' => $module_in_logic_class_item['id'], 
								'name' => $module_in_logic_class_item['name'], 
								'time_limit' => $module_in_logic_class_item['time_limit'], 
								'type' => $module_in_logic_class_item['type'], 
								'parent_id' => $module_in_logic_class_item['parent_id'], 
								'teacher_id' => $module_in_logic_class_item['teacher_id'], 
								'big_lecture_id' => $module_in_logic_class_item['big_lecture_id'],
								'logic_class_id' => $logic_class
								['id'],
								'logic_class_number' => $logic_class
								['number'],
								'logic_class_teacher_id' => $logic_class
								['teacher_id'],
								'logic_class_type' => $logic_class
								['type'],
								'lecture_id' => $logic_class
								['lecture_id'],
								'lecture_name' => $lecture_info['name'],
								'big_lecture_name' => $lecture_info['big_lecture_name']
								);
					array_push($module_add_logic_class, $each_module_add_logic_class);
				}
		}
		//var_dump($module_add_logic_class);
		$data = array();
		$data['title']='考试知识点错误分析';
		$data['teacher']=$teacher;
		$data['class_id']=$class_id;
		$data['module_add_logic_class']=$module_add_logic_class;
		if($class_id)
		{
			$this->load->view('teacher_student/check_module_in_logic_class_student',$data);
		}
		
		else
		{
			$this->load->view('teacher_student/check_module_in_logic_class',$data);
		}
	
	}


	/**
	 * 异步jquery验证
	 * @return [type] [description]
	 */
	function check_student_id_exist()
	{
		$student_id=$this->input->post('student_id');
		$this->load->model('student_model');
		if($this->student_model->student_id_exist($student_id))
			echo json_encode(FALSE);
		else 
			echo json_encode(TRUE);
	}

	/**
	 * CI的表单回调函数验证
	 * @param  [type] $str [description]
	 * @return [type]      [description]
	 */
	function student_id_check($student_id)
	{
		$this->load->model('student_model');
		if($this->student_model->student_id_exist($student_id))
			return FALSE;
		else 
			return TRUE;
	}
	
}
 